﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
using BusinessEntity;
using BusinessEntity.xmglQuerryEntity;
using BusinessRule;
using BusinessRule.Scheduling;
using learun.util;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Nacos.AspNetCore;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ProjectManApi.Extensions;

namespace ProjectManApi.Controllers
{
    //投资计划变更
    //GetXZTZJHList  选择

    //概算
    //gs/GetGSPCIndexList 列表
    //xm/GetXMIndexList 选择 

    //单项
    //lx/GetbidsectionIndexList


    //概算变更
    //gs/GetGSBGIndexList 列表
    //gs/GetGSIndexListN 选择

    //合同变更


    //lx/GetXMDXIndexListC 选择未采购  yg and status = '2'
    //cg/GetXMCGIndexList 采购列表

    //cg/GetXMBBIndexList 编标列表
    //cg/GetXZDXNoBBIndexList 未编标 yg and status = '2'

    //zb/GetZBIndexList 招标
    //zb/GetZBIndexListC unfilled 未招标 yg  View_ProZBLists

    //zb/GetZBDLJGList 招标代理机构 zbdlzt = 0
    //zb/GetNNZBDLJGList 选择 默认zbdlzt =1   View_ProZBDLJGLists

    //ht/GetHTIndexList 合同列表
    //ht/GetHTIndexList 选择 unfilled 

    //ht/GetHTBZJIndexList 保证金保函
    //ht/GetXZHTBZJIndexList 选择保证金保函 

    //ht/GetHTBGDesList 设计变更
    //ht/GetXZHTIndexList 选择设计变更 

    //fk/GetHTFKYJHIndex 付款月计划
    //fk/GetAddHTFKYJHIndex 选择合同

    //jg/GetXMLogsIndexList  现场


    //fk/GetFuKuanSQList 付款申请列表
    //fk/

    //fk/GetAddHTFKYJHIndex 选择合同

    //jg/GetHTQXQList 缺陷期 qxq = 1
    //jg/GetHTQXQList 选择 qxq =0 spstatus =checked

    public class CProManageController : MvcControllerBase
    {
        private readonly INacosServerManager _serverManager;
        public CProManageController(INacosServerManager serverManager) : base(serverManager)
        {
            _serverManager = serverManager;
        }

        #region 导出
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> ExportList(dynamic expjson)
        {

            JObject job = (JObject)expjson;

            List<string> ids = new List<string>();
            List<ColumnModel> cols = new List<ColumnModel>();

            if (null != job["ids"])
                ids = job["ids"].ToObject<string[]>().ToList();

            if (null != job["cols"])
                cols = job["cols"].ToObject<ColumnModel[]>().ToList();

            return File(await CProTZJHRule.GetExportList(ids, cols, BaseEditPower.TableName.项目, "项目列表"), "application/ms-excel", "项目列表.xls");
        }

        #endregion

        #region 项目信息
        /// <summary>
        /// 查询  项目首页报表  Get传参
        /// </summary>
        /// <param name="xmmc"></param>
        /// <param name="xmfl"></param>
        /// <param name="jsdw"></param>
        /// <param name="zjly"></param>
        /// <param name="current"></param>
        /// <param name="size"></param>
        /// <param name="sort"></param>
        /// <param name="column"></param>
        /// <returns></returns>
        [HttpGet]
        //("/{xmmc}/{xxxx}/GetXMIndexList")
        //[Authorize(Policy = "TestPolicy")]
        //[AllowAnonymous]
        //string formCode, string xmmc, string xmflname, string jsdw, string zjly, string year, string status, string current = "1", string size = "20", string sort = "desc", string column = "id"
        public async Task<IActionResult> GetXMIndexList([FromQuery] CProManageQuerry querry)
        {
            try
            {
                //CProManageQuerry querry = new CProManageQuerry();
                //基础属性
                //querry.size = CommonHelper.ParseInt(size);
                //querry.current = CommonHelper.ParseInt(current);
                //querry.sort = sort;
                //querry.column = column;


                //querry.jsdw = jsdw;
                //querry.xmmc = xmmc;
                //querry.xmfl = xmflname;
                //querry.zjly = zjly;

                //querry.year = year;
                //querry.status = status;

                List<string> list = new List<string>();
                (IEnumerable<dynamic>, int) SelectTable = await CProManageRule.GetXMIndexList(querry, await GetDepId());

                foreach (dynamic item in SelectTable.Item1)
                {
                    //这里是id
                    string id = item.id;
                    list.Add(id);
                }

                List<JavaApi.SPSDataItem> spitems = await JavaApi.GetSPStatusJavaAPI(querry.formCode, GetUserAccount(), list, await GetICApiInfo());
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.id;
                    var spstatus = spitems.Where(ii => ii.businessId == id).FirstOrDefault();

                    item.panguData = GetPanGudData(spstatus, item.spstatus);
                }

                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail("响应失败" + ex);
            }
        }


        /// <summary>
        /// 根据id获取单条数据  视图
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpGet]
        //[Authorize(Policy = "TestPolicy")]
        [AllowAnonymous]
        public async Task<IActionResult> GetXMById(string keyValue)
        {
            try
            {
                var SelectTable = await CProManageRule.GetXMByID(keyValue);
                //var vxm = SelectTable.First();
                //计划id
                SelectTable.tzxmids = await CProManageRule.GettzjhByxmid(keyValue);
                return Success(SelectTable);
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }
        /// <summary>
        /// 根据id获取单条数据  表
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpGet]
        //[Authorize(Policy = "TestPolicy")]
        [AllowAnonymous]
        public async Task<IActionResult> GetXMByIdTb(string keyValue)
        {
            try
            {
                var SelectTable = await CProManageRule.GetXMByIDTb(keyValue);

                return Success(SelectTable);
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> GetBNTZJHJZ(string xmid)
        {
            try
            {
                var varret = await BusinessRule.CProManageRule.Gettzjhjzbyyear(xmid, DateTime.Now.Year.ToString());
                return Success(varret);
            }
            catch (Exception ex)
            {
                return Fail("响应失败:" + ex.Message);
            }

        }


        #region 过滤条件  字典

        //项目名称
        /// <summary>
        /// 代建单位
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> GetJsdwNew(string strlx = "unit")
        {
            try
            {
                //string strdepids = await GetDepId();
                //IEnumerable<dynamic> SelectTable = await CProManageRule.GetJsdw(strdepids);

                var SelectTable = await GetDepInfoTree(strlx, "", "project_permission");

                return Success(SelectTable);

                //var strdepids = await GetDepInfo();
                //return Success(strdepids);
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }

        //项目名称
        /// <summary>
        /// 代建单位
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> GetJsdw()
        {
            try
            {
                string strdepids = await GetDepId();
                IEnumerable<dynamic> SelectTable = await CProManageRule.GetJsdw(strdepids);

                //var SelectTable = await GetDepInfoTree();

                return Success(SelectTable);

                //var strdepids = await GetDepInfo();
                //return Success(strdepids);
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }

        /// <summary>
        /// 根据类型获取字典
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetDicByType(string type)
        {
            try
            {
                IEnumerable<BDicInfo> SelectTable = await CProManageRule.GetDicByType<BDicInfo>(type);

                return Success(SelectTable);
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }
        #endregion

        [HttpPost]
        //[AllowAnonymous]
        public async Task<IActionResult> InsertCProManage(dynamic cprojson)
        {
            #region 参数
            //            {
            //                "ope":"add",
            //"tzxmids":[
            //"1",
            //"2"
            //],
            //"cpro":{
            //                    "id": "string11111111111",
            //  "xmbm": "string",
            //  "xmmc": "123321",
            //  "totalMoney": 10,
            //  "xmsx": "string",
            //  "xmnf": "string",
            //  "type": "string",
            //  "buildUnit": "string",
            //  "zjly": "string",
            //  "qt": "string",
            //  "remark": "string",
            //  "begindate": "2021-05-12T07:48:05.862Z",
            //  "enddate": "2021-05-12T07:48:05.862Z",
            //  "levels": "string",
            //  "xmguim": "string",
            //  "adress": "string",
            //  "xyPosition": "string",
            //  "projectManager": "string",
            //  "fzrdh": "string",
            //  "financeManager": "string",
            //  "tel": "string",
            //  "lxspUnit": "string",
            //  "lxspDate": "2021-05-12T07:48:05.862Z",
            //  "lxspwh": "string",
            //  "buildContent": "string",
            //  "process": "string",
            //  "tdzh": "string",
            //  "ydzh": "string",
            //  "ghxkz": "string",
            //  "sgxkz": "string",
            //  "xmfl": "string",
            //  "xmflname": "string",
            //  "spl": "string",
            //  "spr": "string",
            //  "departid": "string",
            //  "status": "string",
            //  "createdate": "2021-05-12T07:48:05.862Z",
            //  "createUser": "string",
            //  "tdfy": 0,
            //  "cd": 0,
            //  "kd": 0,
            //  "lhmj": 0,
            //  "lhl": 0,
            //  "jzdkmj": 0,
            //  "jzrjl": 0,
            //  "jzlhl": 0,
            //  "jzmj": 0,
            //  "jzxg": 0,
            //  "ghzt": "string",
            //  "jhks": "2021-05-12T07:48:05.862Z",
            //  "jhjs": "2021-05-12T07:48:05.862Z",
            //  "cwjssj": "2021-05-12T07:48:05.862Z",
            //  "wqgdsj": "2021-05-12T07:48:05.862Z"
            //},
            //"filecodes":["1","2"]
            //            }
            #endregion

            //var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(cprojson);

            string strope = "add";
            CProManage cpro = new CProManage();
            List<CProManageTZXM> tzxmids = new List<CProManageTZXM>();

            //添加附件上传
            FileUploadent filecodes = new FileUploadent();
            try
            {
                JObject job = (JObject)cprojson;

                //获取方法
                strope = cprojson.ope ?? "";
                strope = strope.ToLower();

                //获取实体类 这样复杂类型不用定义实体类
                cpro = job["cpro"].ToObject<CProManage>();

                if (null != job["tzxmids"])
                    tzxmids = job["tzxmids"].ToObject<List<CProManageTZXM>>();

                //接收附件列表
                if (null != job["apiargs"])
                    filecodes = job["apiargs"].ToObject<FileUploadent>();

                if (strope != "add" && strope != "edit" && strope != "submit")
                {
                    return Fail("参数ope 不合法");
                }
            }
            catch (Exception ex)
            {
                return Fail("参数转换出错：" + ex.Message);
            }

            try
            {
                if (string.IsNullOrEmpty(cpro.xmmc))
                {
                    return Fail("项目名称不能为空");
                }
                if (string.IsNullOrEmpty(cpro.BuildUnit))
                {
                    return Fail("建设单位不能为空");
                }
                if (cpro.TotalMoney == 0)
                {
                    return Fail("项目总投资不能为空");
                }

                if (string.IsNullOrEmpty(cpro.departid))
                {
                    return Fail("项目departid不能为空");
                }

                //if (string.IsNullOrEmpty(cpro.BuildContent))
                //{
                //    return Fail("建设内容不能为空");
                //}

                string strlsope = strope;
                if (strope == "submit")
                {
                    if (string.IsNullOrEmpty(cpro.id))//完成新增
                    {
                        strope = "add";
                    }
                    else
                    {
                        CProManage jhe = await TranstionRule.FindEntityByKey<CProManage>(cpro.id);
                        if (null == jhe)
                        {
                            strope = "add";
                        }
                        else
                        {
                            strope = "edit";
                        }
                    }
                    cpro.status = BaseEditPower.GetSPEnumValue("待审核");
                }

                string strlx = await CProTZJHRule.GetFunGLListBylxEunm(BaseEditPower.JumpModule.投资计划.ToString());
                if (strlx == "1")
                {
                    if (tzxmids.Count <= 0)
                    {
                        return Fail("项目信息需要关联投资计划.");
                    }

                    for (int i = 0; i < tzxmids.Count; i++)
                    {
                        if (string.IsNullOrEmpty(tzxmids[i].txmid))
                        {
                            return Fail("项目信息tzxmids中txmid不能为空.");
                        }
                    }
                }

                if (strope == "add")
                {

                    string strxmid = CommonHelper.GetUniqueId().ToString();
                    cpro.id = strxmid;

                    //项目编码
                    cpro.xmbm = await JavaApi.BusinessBMJavaAPI(BaseEditPower.listTableName[BaseEditPower.TableName.项目.ToString()], Newtonsoft.Json.JsonConvert.SerializeObject(cprojson), await GetICApiInfo());
                    cpro.CreateUser = GetUserName();
                    cpro.createdate = DateTime.Now;

                    for (int i = 0; i < tzxmids.Count; i++)
                    {
                        var kyye = await CProManageRule.GetJHkyye(cpro.id, tzxmids[i].txmid);
                        cpro.jhkyye = kyye.jhkyye;
                    }

                    //权限id
                    //cpro.departid = GetUserDeptid();

                    //bool exists = true;
                    //for (int i = 0; i < tzxmids.Count; i++)
                    //{
                    //    string strtzxmid = tzxmids[i].txmid;
                    //    decimal strje = tzxmids[i].fpje ?? 0;
                    //    if (await CProManageRule.GetTzxmjeVid(strtzxmid, strxmid, strje))
                    //    {
                    //        exists = false;
                    //        break;
                    //    }
                    //}
                    //if (!exists)
                    //{
                    //    return Fail("投资项目分配总金额超出计划金额");
                    //}

                    if (strlsope != "submit")
                        cpro.status = BaseEditPower.GetSPEnumValue("待提交");

                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(cpro.id, filecodes, await GetICApiInfo());

                    bool bsp = true;
                    if (strlsope == "submit")
                    {
                        bsp = false;

                        dynamic _dynamic = new ExpandoObject();
                        _dynamic.TotalMoney = cpro.TotalMoney;
                        _dynamic.xmflname = cpro.xmflname;

                        var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.项目.ToString(), cpro.id);

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = cpro.xmbm, amount = cpro.TotalMoney?.ToString(), content = "[项目]" + cpro.xmmc, deptName = GetUserDeptName() };




                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.项目.ToString(), cpro.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(cpro), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);

                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());
                        cpro.spl = strInstanceId;
                        //if (!bsp)
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    string stradmin = ConfigHelper.GetAppSettings("ServerOp:SerAdmin");
                    await BusinessRule.CProManageRule.InsertCProManage(cpro, tzxmids, GetUserId(), await GetICApiInfo(stradmin));

                    return Success("成功");
                }
                else if (strope == "edit")
                {
                    if (string.IsNullOrEmpty(cpro.id))
                    {
                        return Fail("项目id不能为空");
                    }
                    if (!await BaseEditPower.GetEditPower(cpro.id, BaseEditPower.TableName.项目))
                    {
                        return Fail("响应失败:请确认数据权限");
                    }

                    for (int i = 0; i < tzxmids.Count; i++)
                    {
                        var kyye = await CProManageRule.GetJHkyye(cpro.id, tzxmids[i].txmid);
                        cpro.jhkyye = kyye.jhkyye;
                    }


                    //string strxmid = cpro.ID;

                    //bool exists = true;
                    //for (int i = 0; i < tzxmids.Count; i++)
                    //{
                    //    string strtzxmid = tzxmids[i].txmid;
                    //    decimal strje = tzxmids[i].fpje ?? 0;
                    //    if (!await CProManageRule.GetTzxmjeVid(strtzxmid, strxmid, strje))
                    //    {
                    //        exists = false;
                    //        break;
                    //    }
                    //}
                    //if (!exists)
                    //{
                    //    return Fail("投资项目分配总金额超出计划金额");
                    //}

                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(cpro.id, filecodes, await GetICApiInfo());

                    bool bsp = true;
                    if (strlsope == "submit")
                    {
                        bsp = false;

                        dynamic _dynamic = new ExpandoObject();
                        _dynamic.TotalMoney = cpro.TotalMoney;
                        _dynamic.xmflname = cpro.xmflname;

                        var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.项目.ToString(), cpro.id);
                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = cpro.xmbm, amount = cpro.TotalMoney?.ToString(), content = "[项目]" + cpro.xmmc, deptName = GetUserDeptName() };

                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.项目.ToString(), cpro.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(cpro), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);
                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        //if (!bsp)
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());
                        cpro.spl = strInstanceId;
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    await BusinessRule.CProManageRule.UpdateCProManage(cpro, tzxmids, GetUserId());

                    return Success("成功");
                }


                else
                {
                    return Fail("ope：" + strope + " ,无法识别");
                }
            }
            catch (Exception ex)
            {
                return Fail("响应失败:" + ex.Message);
            }
        }

        /// <summary>
        /// 删除单条
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> DeleteCProManage(string keyValue)
        {
            try
            {
                await BusinessRule.CProManageRule.DeleteCProManage(keyValue);

                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        /// <summary>
        /// 删除多条数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> DeleteCProManageMulti(Root ids)
        {
            try
            {
                await BusinessRule.CProManageRule.DeleteCProManageMulti(ids);

                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        #region 树状节点列表
        ///// <summary>
        ///// 一级菜单
        ///// </summary>
        ///// <returns></returns>
        //[HttpGet]
        //[AllowAnonymous]
        //public async Task<IActionResult> GetTreeByUserId()
        //{
        //    try
        //    {
        //        var SelectTable = await CProManageRule.GetTreeByUserId(GetUserId());

        //        return Success(SelectTable);
        //    }
        //    catch (Exception)
        //    {
        //        return Fail("响应失败");
        //    }
        //}
        ///// <summary>
        ///// 二级菜单
        ///// </summary>
        ///// <param name="Caid"></param>
        ///// <returns></returns>
        //[HttpGet]
        //[AllowAnonymous]
        //public async Task<IActionResult> GetTreeExtByCaId(string Caid)
        //{
        //    try
        //    {
        //        var SelectTable = await CProManageRule.GetTreeExtByCaId(GetUserId(), Caid);

        //        return Success(SelectTable);
        //    }
        //    catch (Exception)
        //    {
        //        return Fail("响应失败");
        //    }
        //}
        ///// <summary>
        ///// 三级菜单
        ///// </summary>
        ///// <param name="Caid"></param>
        ///// <returns></returns>
        //[HttpGet]
        //[AllowAnonymous]
        //public async Task<IActionResult> GetAllFunctionByExt(string Extid)
        //{
        //    try
        //    {
        //        var SelectTable = await CProManageRule.GetAllFunctionByExt(GetUserId(), Extid);

        //        return Success(SelectTable);
        //    }
        //    catch (Exception)
        //    {
        //        return Fail("响应失败");
        //    }
        //}
        ///// <summary>
        ///// 获取主菜单树结构列表
        ///// </summary>
        ///// <param name="xmid"></param>
        ///// <returns></returns>
        //[HttpGet]
        //[AllowAnonymous]
        //public async Task<IActionResult> GetFuncTreeByUid()
        //{
        //    try
        //    {
        //        var SelectTable = await CProManageRule.GetFuncTreeByUid(GetUserId());
        //        return Success(SelectTable);
        //    }
        //    catch (Exception ex)
        //    {
        //        return Fail(ex.Message);
        //    }
        //}
        #endregion

        #region 项目前期  成果上传节点列表、甘特图列表、甘特图勾选列表、甘特图勾选更新、报规报批新增修改、报规报批删除
        [HttpGet]
        public async Task<IActionResult> GetXMQQIndexList(string xmmc, string xmfl, string jsdw, string sftb, string year, string status, string current = "1", string size = "20", string sort = "desc", string column = "id")
        {
            try
            {
                CProManageQQQuerry querry = new CProManageQQQuerry();
                //基础属性
                querry.size = CommonHelper.ParseInt(size);
                querry.current = CommonHelper.ParseInt(current);
                querry.sort = sort;
                querry.column = column;


                querry.Jsdw = jsdw;
                querry.Xmmc = xmmc;
                querry.Xmfl = xmfl;
                querry.sftb = sftb;

                querry.year = year;
                querry.status = status;

                (IEnumerable<dynamic>, int) SelectTable = await CProManageRule.GetXMQQIndexList(querry, await GetDepId());

                //List<string> list = new List<string>();
                //foreach (dynamic item in SelectTable.Item1)
                //{
                //    //这里是id
                //    string id = item.id;
                //    list.Add(id);
                //}

                //List<JavaApi.SPSDataItem> spitems = await JavaApi.GetSPStatusJavaAPI(formCode, GetUserAccount(), list, await GetICApiInfo());
                //foreach (dynamic item in SelectTable.Item1)
                //{
                //    string id = item.id;
                //    var spstatus = spitems.Where(ii => ii.businessId == id).FirstOrDefault();

                //    item.panguData = GetPanGudData(spstatus, item.spstatus);
                //}

                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail("响应失败" + ex);
            }
        }

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> insertXMQQMulti(List<CProXMQQ> xmqqs)
        {
            //string strope = "add";
            //List<CProXMQQ> xmqqs = null;
            //try
            //{
            //    JObject job = (JObject)xmqqjson;

            //    //获取方法
            //    strope = xmqqjson.ope;
            //    strope = strope.ToLower();

            //    //获取实体类 这样复杂类型不用定义实体类
            //    xmqqs = job["xmqqs"].ToObject<List<CProXMQQ>>();
            //}
            //catch (Exception ex)
            //{
            //    return Fail("参数转换出错：" + ex.Message);
            //}

            try
            {
                //string strid = CommonHelper.GetUniqueId().ToString();
                //xmqqjson.id = strid;
                //xmqqjson.mark = "0";


                for (int i = 0; i < xmqqs.Count; i++)
                {
                    if (string.IsNullOrEmpty(xmqqs[i].xmid))
                    {
                        return Fail("节点集合中节点xmid不能为空");
                    }
                    if (string.IsNullOrEmpty(xmqqs[i].pid))
                    {
                        return Fail("节点集合中节点pid不能为空");
                    }
                }

                await BusinessRule.CProManageRule.AUDXMQQ(xmqqs, "add");

                return Success("成功");
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }

        /// <summary>
        /// 新增项目前期节点的模板
        /// </summary>
        /// <param name="xmqqjson"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> InsertXMQQMB(dynamic xmqqjson)
        {

            string strope = "add";//edit
            CProXMQQ xmqq = new CProXMQQ();
            //List<CProManageTZXM> tzxmids = new List<CProManageTZXM>();

            //添加附件上传
            FileUploadent filecodes = new FileUploadent();
            try
            {
                JObject job = (JObject)xmqqjson;

                //获取方法
                strope = xmqqjson.ope ?? "";
                strope = strope.ToLower();

                //获取实体类 这样复杂类型不用定义实体类
                xmqq = job["xmqq"].ToObject<CProXMQQ>();

                //if (null != job["tzxmids"])
                //    tzxmids = job["tzxmids"].ToObject<List<CProManageTZXM>>();

                //接收附件列表
                if (null != job["apiargs"])
                    filecodes = job["apiargs"].ToObject<FileUploadent>();


                if (strope != "add" && strope != "edit" && strope != "submit")
                {
                    return Fail("参数ope 不合法");
                }

            }
            catch (Exception ex)
            {
                return Fail("参数转换出错：" + ex.Message);
            }


            try
            {
                xmqq.xmid = "0";//模板 xmid = 0 是固定的

                //if (string.IsNullOrEmpty(xmqq.lx))
                //{
                //    return Fail("项目节点信息lx不能为空");
                //}

                if (strope == "add")
                {
                    if (string.IsNullOrEmpty(xmqq.pid))
                    {
                        return Fail("项目节点信息pid不能为空");
                    }
                    if (string.IsNullOrEmpty(xmqq.xmid))
                    {
                        return Fail("项目节点信息xmid不能为空");
                    }
                    if (string.IsNullOrEmpty(xmqq.name))
                    {
                        return Fail("项目节点信息name不能为空");
                    }
                    if (xmqq.arr == 0)
                    {
                        return Fail("项目节点信息arr不能为空");
                    }



                    string strid = CommonHelper.GetUniqueId().ToString();


                    xmqq.id = strid;
                    xmqq.mark = "0";

                    //项目前期节点
                    xmqq.bm = await JavaApi.BusinessBMJavaAPI("xmgsfy", Newtonsoft.Json.JsonConvert.SerializeObject(xmqqjson), await GetICApiInfo());

                    //添加子节点
                    if (xmqq.pid != "0")
                    {
                        if (string.IsNullOrEmpty(xmqq.lx))
                        {

                            string strsql = @"with cc as
(
select * from CProXMQQ where id = '" + xmqq.pid + @"'
union all
select CProXMQQ.* from CProXMQQ inner join cc  on CProXMQQ.id = cc.pid
)

select * from cc";
                            DataTable dts = await TranstionRule.ProcessSql(strsql);
                            for (int i = 0; i < dts.Rows.Count; i++)
                            {
                                string lx = dts.Rows[i]["lx"].ToString();
                                if (!string.IsNullOrEmpty(lx))
                                {
                                    xmqq.lx = lx;
                                    break;
                                }
                            }

                            //return Fail("项目节点信息pid不能为空");
                        }
                    }

                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(xmqq.id, filecodes, await GetICApiInfo());

                    await BusinessRule.CProManageRule.AUDXMQQ(xmqq, "add");
                }
                else
                {
                    if (string.IsNullOrEmpty(xmqq.id))
                    {
                        return Fail("节点id不能为空");
                    }
                    if (string.IsNullOrEmpty(xmqq.pid))
                    {
                        return Fail("项目节点信息pid不能为空");
                    }
                    if (string.IsNullOrEmpty(xmqq.xmid))
                    {
                        return Fail("项目节点信息xmid不能为空");
                    }
                    if (string.IsNullOrEmpty(xmqq.name))
                    {
                        return Fail("项目节点信息name不能为空");
                    }
                    if (xmqq.arr == 0)
                    {
                        return Fail("项目节点信息arr不能为空");
                    }

                    //xmqq.arr = null;

                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(xmqq.id, filecodes, await GetICApiInfo());

                    await BusinessRule.CProManageRule.AUDXMQQ(xmqq, "edit");
                }

                return Success("成功");
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }

        /// <summary>
        /// 新增项目前期节点
        /// </summary>
        /// <param name="xmqqjson"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> InsertXMQQ(dynamic xmqqjson)
        {

            string strope = "add";
            CProXMQQ xmqq = new CProXMQQ();
            //List<CProManageTZXM> tzxmids = new List<CProManageTZXM>();

            //添加附件上传
            FileUploadent filecodes = new FileUploadent();
            try
            {
                JObject job = (JObject)xmqqjson;

                //获取方法
                strope = xmqqjson.ope ?? "";
                strope = strope.ToLower();

                //获取实体类 这样复杂类型不用定义实体类
                xmqq = job["xmqq"].ToObject<CProXMQQ>();

                //if (null != job["tzxmids"])
                //    tzxmids = job["tzxmids"].ToObject<List<CProManageTZXM>>();

                //接收附件列表
                if (null != job["apiargs"])
                    filecodes = job["apiargs"].ToObject<FileUploadent>();


                if (strope != "add" && strope != "edit" && strope != "submit")
                {
                    return Fail("参数ope 不合法");
                }

            }
            catch (Exception ex)
            {
                return Fail("参数转换出错：" + ex.Message);
            }


            try
            {
                //if (string.IsNullOrEmpty(xmqq.lx))
                //{
                //    return Fail("项目节点信息lx不能为空");
                //}

                if (strope == "add")
                {
                    if (string.IsNullOrEmpty(xmqq.pid))
                    {
                        return Fail("项目节点信息pid不能为空");
                    }
                    if (string.IsNullOrEmpty(xmqq.xmid))
                    {
                        return Fail("项目节点信息xmid不能为空");
                    }
                    if (string.IsNullOrEmpty(xmqq.name))
                    {
                        return Fail("项目节点信息name不能为空");
                    }

                    if (null == xmqq.arr)//|| xmqq.arr < 0
                    {
                        return Fail("项目节点信息arr不能为空");
                    }

                    //添加子节点
                    if (xmqq.pid != "0")
                    {
                        if (string.IsNullOrEmpty(xmqq.lx))
                        {

                            string strsql = @"with cc as
(
select * from CProXMQQ where id = '" + xmqq.pid + @"'
union all
select CProXMQQ.* from CProXMQQ inner join cc  on CProXMQQ.id = cc.pid
)

select * from cc";
                            DataTable dts = await TranstionRule.ProcessSql(strsql);
                            for (int i = 0; i < dts.Rows.Count; i++)
                            {
                                string lx = dts.Rows[i]["lx"].ToString();
                                if (!string.IsNullOrEmpty(lx))
                                {
                                    xmqq.lx = lx;
                                    break;
                                }
                            }

                            //return Fail("项目节点信息pid不能为空");
                        }
                    }


                    string strid = CommonHelper.GetUniqueId().ToString();
                    xmqq.id = strid;
                    xmqq.mark = "0";
                    xmqq.bm = await JavaApi.BusinessBMJavaAPI(BaseEditPower.listTableName[BaseEditPower.TableName.项目前期.ToString()], Newtonsoft.Json.JsonConvert.SerializeObject(xmqqjson), await GetICApiInfo());
                    //await BusinessRule.Assets.CreateZCDM("XM", "XMJD");

                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(xmqq.id, filecodes, await GetICApiInfo());

                    await BusinessRule.CProManageRule.AUDXMQQ(xmqq, "add");
                }
                else
                {
                    if (string.IsNullOrEmpty(xmqq.id))
                    {
                        return Fail("节点id不能为空");
                    }
                    if (string.IsNullOrEmpty(xmqq.pid))
                    {
                        return Fail("项目节点信息pid不能为空");
                    }
                    if (string.IsNullOrEmpty(xmqq.xmid))
                    {
                        return Fail("项目节点信息xmid不能为空");
                    }
                    if (string.IsNullOrEmpty(xmqq.name))
                    {
                        return Fail("项目节点信息name不能为空");
                    }

                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(xmqq.id, filecodes, await GetICApiInfo());

                    await BusinessRule.CProManageRule.AUDXMQQ(xmqq, "edit");
                }

                return Success("成功");
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }

        /// <summary>
        /// 修改项目前期节点
        /// </summary>
        /// <param name="xmqqjson"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> UpdateXMQQ(CProXMQQ xmqqjson)
        {
            try
            {
                if (string.IsNullOrEmpty(xmqqjson.id))
                {
                    return Fail("节点id不能为空");
                }

                xmqqjson.arr = null;
                await BusinessRule.CProManageRule.AUDXMQQ(xmqqjson, "edit");

                return Success("成功");
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }

        /// <summary>
        /// 删除多条前期节点
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> DeleteXMQQMulti(Root ids)
        {
            try
            {
                //if (string.IsNullOrEmpty(xmqqjson.id))
                //{
                //    return Fail("节点id不能为空");
                //}

                await BusinessRule.CProManageRule.DeleteCProXMQQMulti(ids);

                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        ///// <summary>
        ///// xmid = 0  是基础的  根据xmid 获取项目前期节点
        ///// </summary>
        ///// <param name="xmid"></param>
        ///// <returns></returns>
        //[HttpGet]
        //[AllowAnonymous]
        //public async Task<IActionResult> GetXMQQAllTreeByXmid(string xmid)
        //{
        //    try
        //    {
        //        if (string.IsNullOrEmpty(xmid))
        //        {
        //            return Fail("参数xmid不能为空");
        //        }

        //        var SelectTable = await CProManageRule.GetXMQQTreeByXmid(xmid, "");//GetXMQQByXmid

        //        return Success(SelectTable);
        //    }
        //    catch (Exception)
        //    {
        //        return Fail("响应失败");
        //    }
        //}

        //项目id 类型id
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetExportTreeNodeFromBase(string xmid, string lx)
        {
            try
            {
                if (string.IsNullOrEmpty(xmid))
                {
                    return Fail("参数xmid不能为空");
                }
                if (string.IsNullOrEmpty(lx))
                {
                    return Fail("参数lx不能为空");
                }

                var GetXMTreeNodes = await CProManageRule.GetXMTreeNodeByXmid(xmid, lx);//GetXMQQByXmid
                if (GetXMTreeNodes.Count > 0)
                {
                    return Fail("该项目下已经存在节点，不能导入");
                }

                var ExportRestlt = await CProManageRule.GetExportTreeNodeFromBase(xmid, lx);//GetXMQQByXmid

                return Success("操作成功");
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }

        /// <summary>
        /// 这个类型是 lx 查询 qq xc 和jg的区分
        /// </summary>
        /// <param name="xmid"></param>
        /// <param name="lx"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetXMTreeNodeByXmid(string xmid, string lx)
        {
            try
            {
                if (string.IsNullOrEmpty(xmid))
                {
                    return Fail("参数xmid不能为空");
                }
                //if (string.IsNullOrEmpty(lx))
                //{
                //    return Fail("参数lx不能为空");
                //}

                var SelectTable = await CProManageRule.GetXMTreeNodeByXmid(xmid, lx);//GetXMQQByXmid

                return Success(SelectTable);
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }

        /// <summary>
        /// xmid = 0  是基础的  根据xmid 获取项目前期节点 这个是查询名称的
        /// </summary>
        /// <param name="xmid"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetXMQQTreeByXmid(string xmid, string lx)
        {
            try
            {
                if (string.IsNullOrEmpty(xmid))
                {
                    return Fail("参数xmid不能为空");
                }
                //if (string.IsNullOrEmpty(lx))
                //{
                //    return Fail("参数lx不能为空");
                //}

                var SelectTable = await CProManageRule.GetXMQQTreeByXmid(xmid, lx);//GetXMQQByXmid

                return Success(SelectTable);
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }

        /// <summary>
        /// xmid = 0  是基础的  根据xmid 获取项目前期节点
        /// </summary>
        /// <param name="xmid"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetXMQQByXmidAndID(string xmid, string id)
        {
            try
            {
                if (string.IsNullOrEmpty(xmid))
                {
                    return Fail("参数xmid不能为空");
                }
                //if (string.IsNullOrEmpty(lx))
                //{
                //    return Fail("参数lx不能为空");
                //}

                var SelectTable = await CProManageRule.GetXMQQByXmidAndID(xmid, id);//GetXMQQByXmid

                return Success(SelectTable);
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }

        /// <summary>
        /// 根据id 查询节点本身 查询表
        /// </summary>
        /// <param name="xmid"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetXMQQSigByXmidAndID(string xmid, string id)
        {
            try
            {
                if (string.IsNullOrEmpty(xmid))
                {
                    return Fail("参数xmid不能为空");
                }
                //if (string.IsNullOrEmpty(lx))
                //{
                //    return Fail("参数lx不能为空");
                //}

                var SelectTable = await CProManageRule.GetXMQQSigByXmidAndID(xmid, id);//GetXMQQByXmid

                return Success(SelectTable);
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }

        /// <summary>
        /// 根据id 查询节点本身 查询表
        /// </summary>
        /// <param name="xmid"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetXMQQSigByID(string id)
        {
            try
            {
                if (string.IsNullOrEmpty(id))
                {
                    return Fail("参数id不能为空");
                }

                var SelectTable = await CProManageRule.GetXMQQSigByID(id);//GetXMQQByXmid

                return Success(SelectTable);
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }


        /// <summary>
        /// xmid = 0  是基础的  根据xmid 获取项目前期节点
        /// </summary>
        /// <param name="xmid"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetXMQQAllByXmid(string xmid, string lx, string id)
        {
            try
            {
                if (string.IsNullOrEmpty(xmid))
                {
                    return Fail("参数xmid不能为空");
                }

                var SelectTable = await CProManageRule.GetXMQQAllByXmid(xmid, lx, id);//GetXMQQByXmid

                return Success(SelectTable);
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }


        /// <summary>
        /// xmid = 0  是基础的  根据xmid 获取项目前期节点 甘特图
        /// </summary>
        /// <param name="xmid"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetXMGanttNodeByXmid(string xmid, string lx)
        {
            try
            {
                if (string.IsNullOrEmpty(xmid))
                {
                    return Fail("参数xmid不能为空");
                }

                var SelectTable = await CProManageRule.GetXMGanttNodeByXmid(xmid, lx);//GetXMQQByXmid

                return Success(SelectTable);
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }

        #endregion

        #endregion

        #region 计划开工
        [HttpGet]
        public async Task<IActionResult> GetXMIndexListHasNoStart([FromQuery] CProManageQuerry querry)
        {
            try
            {
                //状态是已审核
                querry.status = "checked";

                List<string> list = new List<string>();
                (IEnumerable<dynamic>, int) SelectTable = await CProManageRule.GetXMIndexListHasNoStart(querry, await GetDepId());

                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail("响应失败" + ex);
            }
        }

        [HttpGet]
        public async Task<IActionResult> GetPlancommenIndexList([FromQuery] CProPlancommenQuerry querry)
        {
            try
            {
                List<string> list = new List<string>();
                (IEnumerable<dynamic>, int) SelectTable = await CProManageRule.GetPlancommenindexList(querry, await GetDepId());

                foreach (dynamic item in SelectTable.Item1)
                {
                    //这里是id
                    string id = item.id;
                    list.Add(id);
                }

                List<JavaApi.SPSDataItem> spitems = await JavaApi.GetSPStatusJavaAPI(querry.formCode, GetUserAccount(), list, await GetICApiInfo());
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.id;
                    var spstatus = spitems.Where(ii => ii.businessId == id).FirstOrDefault();

                    item.panguData = GetPanGudData(spstatus, item.spstatus);
                }

                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail("响应失败" + ex);
            }
        }

        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetPlancommenByid(string planid)
        {
            try
            {
                var Plancommen = await CProManageRule.GetPlancommenByid(planid);
                return Success(Plancommen);
            }
            catch (Exception ex)
            {
                return Fail("响应失败" + ex);
            }
        }

        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetPlancommenByxmid(string xmid)
        {
            try
            {
                var Plancommen = await CProManageRule.GetPlancommenByxmid(xmid);
                return Success(Plancommen);
            }
            catch (Exception ex)
            {
                return Fail("响应失败" + ex);
            }
        }

        [HttpPost]
        //[AllowAnonymous]
        public async Task<IActionResult> InsertCProPlancommen(dynamic cplanjson)
        {
            #region 参数
            //            {
            //                "ope":"add",
            //"tzxmids":[
            //"1",
            //"2"
            //],
            //"cpro":{
            //                    "id": "string11111111111",
            //  "xmbm": "string",
            //  "xmmc": "123321",
            //  "totalMoney": 10,
            //  "xmsx": "string",
            //  "xmnf": "string",
            //  "type": "string",
            //  "buildUnit": "string",
            //  "zjly": "string",
            //  "qt": "string",
            //  "remark": "string",
            //  "begindate": "2021-05-12T07:48:05.862Z",
            //  "enddate": "2021-05-12T07:48:05.862Z",
            //  "levels": "string",
            //  "xmguim": "string",
            //  "adress": "string",
            //  "xyPosition": "string",
            //  "projectManager": "string",
            //  "fzrdh": "string",
            //  "financeManager": "string",
            //  "tel": "string",
            //  "lxspUnit": "string",
            //  "lxspDate": "2021-05-12T07:48:05.862Z",
            //  "lxspwh": "string",
            //  "buildContent": "string",
            //  "process": "string",
            //  "tdzh": "string",
            //  "ydzh": "string",
            //  "ghxkz": "string",
            //  "sgxkz": "string",
            //  "xmfl": "string",
            //  "xmflname": "string",
            //  "spl": "string",
            //  "spr": "string",
            //  "departid": "string",
            //  "status": "string",
            //  "createdate": "2021-05-12T07:48:05.862Z",
            //  "createUser": "string",
            //  "tdfy": 0,
            //  "cd": 0,
            //  "kd": 0,
            //  "lhmj": 0,
            //  "lhl": 0,
            //  "jzdkmj": 0,
            //  "jzrjl": 0,
            //  "jzlhl": 0,
            //  "jzmj": 0,
            //  "jzxg": 0,
            //  "ghzt": "string",
            //  "jhks": "2021-05-12T07:48:05.862Z",
            //  "jhjs": "2021-05-12T07:48:05.862Z",
            //  "cwjssj": "2021-05-12T07:48:05.862Z",
            //  "wqgdsj": "2021-05-12T07:48:05.862Z"
            //},
            //"filecodes":["1","2"]
            //            }
            #endregion

            string strope = "add";
            CProManageJHKG cpro = new CProManageJHKG();
            //List<CProManageTZXM> tzxmids = new List<CProManageTZXM>();

            //添加附件上传
            FileUploadent filecodes = new FileUploadent();
            try
            {
                JObject job = (JObject)cplanjson;

                //获取方法
                strope = cplanjson.ope ?? "";
                strope = strope.ToLower();

                //获取实体类 这样复杂类型不用定义实体类
                cpro = job["cplan"].ToObject<CProManageJHKG>();

                //接收附件列表
                if (null != job["apiargs"])
                    filecodes = job["apiargs"].ToObject<FileUploadent>();

                if (strope != "add" && strope != "edit" && strope != "submit")
                {
                    return Fail("参数ope 不合法");
                }
            }
            catch (Exception ex)
            {
                return Fail("参数转换出错：" + ex.Message);
            }

            try
            {
                if (string.IsNullOrEmpty(cpro.xmid))
                {
                    return Fail("项目id不能为空");
                }
                if (null == cpro.jhkgrq.ToDateOrNull())
                {
                    return Fail("计划开工日期不能为空");
                }

                string strlsope = strope;
                if (strope == "submit")
                {
                    if (string.IsNullOrEmpty(cpro.id))//完成新增
                    {
                        strope = "add";
                    }
                    else
                    {
                        CProManageJHKG jhe = await TranstionRule.FindEntityByKey<CProManageJHKG>(cpro.id);
                        if (null == jhe)
                        {
                            strope = "add";
                        }
                        else
                        {
                            strope = "edit";
                        }
                    }
                    cpro.status = BaseEditPower.GetSPEnumValue("待审核");
                }

                if (strope == "add")
                {

                    string strxmid = CommonHelper.GetUniqueId().ToString();
                    cpro.id = strxmid;

                    //项目编码
                    cpro.jhbm = await JavaApi.BusinessBMJavaAPI(BaseEditPower.listTableName[BaseEditPower.TableName.项目计划开工.ToString()], Newtonsoft.Json.JsonConvert.SerializeObject(cplanjson), await GetICApiInfo());
                    cpro.CreateUser = GetUserName();

                    if (strlsope != "submit")
                        cpro.status = BaseEditPower.GetSPEnumValue("待提交");

                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(cpro.id, filecodes, await GetICApiInfo());

                    bool bsp = true;
                    if (strlsope == "submit")
                    {
                        bsp = false;

                        var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.项目计划开工.ToString(), cpro.id);

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = cpro.jhbm, amount = "", content = "[项目计划开工]" + "", deptName = GetUserDeptName() };

                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.项目计划开工.ToString(), cpro.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(cplanjson), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);

                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());
                        cpro.spl = strInstanceId;
                        //if (!bsp)
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    await BusinessRule.CProManageRule.InsertCProPlancommen(cpro, "add");

                    return Success("成功");
                }
                else if (strope == "edit")
                {
                    if (string.IsNullOrEmpty(cpro.id))
                    {
                        return Fail("项目id不能为空");
                    }
                    if (!await BaseEditPower.GetEditPower(cpro.id, BaseEditPower.TableName.项目计划开工))
                    {
                        return Fail("响应失败:请确认数据权限");
                    }


                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(cpro.id, filecodes, await GetICApiInfo());

                    bool bsp = true;
                    if (strlsope == "submit")
                    {
                        bsp = false;

                        var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.项目计划开工.ToString(), cpro.id);

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = cpro.jhbm, amount = "", content = "[项目计划开工]" + "", deptName = GetUserDeptName() };

                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.项目计划开工.ToString(), cpro.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(cplanjson), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);

                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());
                        cpro.spl = strInstanceId;
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    await BusinessRule.CProManageRule.InsertCProPlancommen(cpro, "edit");

                    return Success("成功");
                }


                else
                {
                    return Fail("ope：" + strope + " ,无法识别");
                }
            }
            catch (Exception ex)
            {
                return Fail("响应失败:" + ex.Message);
            }
        }

        /// <summary>
        /// 根据id删除多条数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> DeletePlancommenMulti(Root ids)
        {
            try
            {
                foreach (BaseDeleteMulti idm in ids.ids)
                {
                    if (!await BaseEditPower.GetEditPower(idm.id, BaseEditPower.TableName.项目计划开工))
                    {
                        throw new Exception("请确认数据权限");
                    }
                }

                await BusinessRule.CProManageRule.DeletePlancommenMulti(ids);

                return Success("成功");
            }
            catch (Exception ex)
            {
                //_logger.Error(ids.ToJson() + "||" + ex.Message);
                return Fail(ex.Message);
            }
        }
        #endregion

        #region 常州预算
        [HttpGet]
        public async Task<IActionResult> GetXZXMList([FromQuery] CProManageQuerry querry)
        {
            try
            {
                //状态是已审核
                querry.status = "checked";

                List<string> list = new List<string>();
                (IEnumerable<dynamic>, int) SelectTable = await CProManageRule.GetXZXMList(querry, await GetDepId());

                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail("响应失败" + ex);
            }
        }


        [HttpGet]
        public async Task<IActionResult> GetYSList([FromQuery] CProYSQuerry querry)
        {
            try
            {
                List<string> list = new List<string>();
                (IEnumerable<dynamic>, int) SelectTable = await CProManageRule.GetYSList(querry, await GetDepId());

                foreach (dynamic item in SelectTable.Item1)
                {
                    //这里是id
                    string id = item.id;
                    list.Add(id);
                }

                List<JavaApi.SPSDataItem> spitems = await JavaApi.GetSPStatusJavaAPI(querry.formCode, GetUserAccount(), list, await GetICApiInfo());
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.id;
                    var spstatus = spitems.Where(ii => ii.businessId == id).FirstOrDefault();

                    item.panguData = GetPanGudData(spstatus, item.spstatus);
                }

                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail("响应失败" + ex);
            }
        }

        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetYSByid(string ysid)
        {
            try
            {
                var ysxx = await CProManageRule.GetYSByid(ysid);
                return Success(ysxx);
            }
            catch (Exception ex)
            {
                return Fail("响应失败" + ex);
            }
        }

        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetYSByxmid(string xmid)
        {
            try
            {
                var ysxx = await CProManageRule.GetYSByxmid(xmid);
                return Success(ysxx);
            }
            catch (Exception ex)
            {
                return Fail("响应失败" + ex);
            }
        }

        [HttpPost]
        //[AllowAnonymous]
        public async Task<IActionResult> InsertCProYS([FromBody] xmysCRUD cplanjson)//dynamic cplanjson
        {
            string strope = "add";
            CProManCZBK cpro = new CProManCZBK();


            //添加附件上传
            FileUploadent filecodes = new FileUploadent();
            try
            {
                //JObject job = (JObject)cplanjson;

                //获取方法
                strope = cplanjson.ope ?? "";
                strope = strope.ToLower();

                cpro = cplanjson.ys;
                if (null != cplanjson.apiargs)
                    filecodes = cplanjson.apiargs;

                //获取实体类 这样复杂类型不用定义实体类
                //cpro = job["cplan"].ToObject<CProManCZBK>();

                ////接收附件列表
                //if (null != job["apiargs"])
                //    filecodes = job["apiargs"].ToObject<FileUploadent>();

                if (strope != "add" && strope != "edit" && strope != "submit")
                {
                    return Fail("参数ope 不合法");
                }
            }
            catch (Exception ex)
            {
                return Fail("参数转换出错：" + ex.Message);
            }

            try
            {
                if (string.IsNullOrEmpty(cpro.xmid))
                {
                    return Fail("项目id不能为空");
                }
                if (null == cpro.nd)
                {
                    return Fail("预算年度nd不能为空");
                }

                string strlsope = strope;
                if (strope == "submit")
                {
                    if (string.IsNullOrEmpty(cpro.id))//完成新增
                    {
                        strope = "add";
                    }
                    else
                    {
                        CProManCZBK jhe = await TranstionRule.FindEntityByKey<CProManCZBK>(cpro.id);
                        if (null == jhe)
                        {
                            strope = "add";
                        }
                        else
                        {
                            strope = "edit";
                        }
                    }
                    cpro.status = BaseEditPower.GetSPEnumValue("待审核");
                }

                if (strope == "add")
                {

                    string strxmid = CommonHelper.GetUniqueId().ToString();
                    cpro.id = strxmid;
                    cpro.tbr = GetUserName();
                    cpro.rq = DateTime.Now;
                    //项目编码
                    cpro.ysbm = await JavaApi.BusinessBMJavaAPI(BaseEditPower.listTableName[BaseEditPower.TableName.项目预算.ToString()], Newtonsoft.Json.JsonConvert.SerializeObject(cplanjson), await GetICApiInfo());
                    cpro.createuser = GetUserName();

                    if (strlsope != "submit")
                        cpro.status = BaseEditPower.GetSPEnumValue("待提交");

                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(cpro.id, filecodes, await GetICApiInfo());

                    bool bsp = true;
                    if (strlsope == "submit")
                    {
                        bsp = false;

                        var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.项目预算.ToString(), cpro.id);

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = cpro.ysbm, amount = cpro.je?.ToString() ?? "", content = "[项目预算]" + "", deptName = GetUserDeptName() };

                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.项目预算.ToString(), cpro.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(cplanjson), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);

                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());
                        cpro.spl = strInstanceId;
                        //if (!bsp)
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    await BusinessRule.CProManageRule.InsertCProYS(cpro, "add");

                    return Success("成功");
                }
                else if (strope == "edit")
                {
                    if (string.IsNullOrEmpty(cpro.id))
                    {
                        return Fail("id不能为空");
                    }
                    if (!await BaseEditPower.GetEditPower(cpro.id, BaseEditPower.TableName.项目预算))
                    {
                        return Fail("响应失败:请确认数据权限");
                    }


                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(cpro.id, filecodes, await GetICApiInfo());

                    bool bsp = true;
                    if (strlsope == "submit")
                    {
                        bsp = false;

                        var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.项目预算.ToString(), cpro.id);

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = cpro.ysbm, amount = cpro.je?.ToString() ?? "", content = "[项目预算]" + "", deptName = GetUserDeptName() };

                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.项目预算.ToString(), cpro.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(cplanjson), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);

                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());
                        cpro.spl = strInstanceId;
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    await BusinessRule.CProManageRule.InsertCProYS(cpro, "edit");

                    return Success("成功");
                }


                else
                {
                    return Fail("ope：" + strope + " ,无法识别");
                }
            }
            catch (Exception ex)
            {
                return Fail("响应失败:" + ex.Message);
            }
        }

        /// <summary>
        /// 根据id删除多条数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> DeleteYSMulti(Root ids)
        {
            try
            {
                foreach (BaseDeleteMulti idm in ids.ids)
                {
                    if (!await BaseEditPower.GetEditPower(idm.id, BaseEditPower.TableName.项目预算))
                    {
                        throw new Exception("请确认数据权限");
                    }
                }

                await BusinessRule.CProManageRule.DeleteYSMulti(ids);

                return Success("成功");
            }
            catch (Exception ex)
            {
                //_logger.Error(ids.ToJson() + "||" + ex.Message);
                return Fail(ex.Message);
            }
        }



        #endregion

        #region 常州预算变更
        [HttpGet]
        public async Task<IActionResult> GetXZYSList([FromQuery] CProXZYSQuerry querry)
        {
            try
            {
                //状态是已审核
                querry.status = "checked";

                List<string> list = new List<string>();
                (IEnumerable<dynamic>, int) SelectTable = await CProManageRule.GetXZYSList(querry, await GetDepId());

                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail("响应失败" + ex);
            }
        }


        [HttpGet]
        public async Task<IActionResult> GetYSBGList([FromQuery] CProYSBGQuerry querry)
        {
            try
            {
                List<string> list = new List<string>();
                (IEnumerable<dynamic>, int) SelectTable = await CProManageRule.GetYSBGList(querry, await GetDepId());

                foreach (dynamic item in SelectTable.Item1)
                {
                    //这里是id
                    string id = item.id;
                    list.Add(id);
                }

                List<JavaApi.SPSDataItem> spitems = await JavaApi.GetSPStatusJavaAPI(querry.formCode, GetUserAccount(), list, await GetICApiInfo());
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.id;
                    var spstatus = spitems.Where(ii => ii.businessId == id).FirstOrDefault();

                    item.panguData = GetPanGudData(spstatus, item.spstatus);
                }

                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail("响应失败" + ex);
            }
        }

        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> Getysbgbyid(string bgid)
        {
            try
            {
                var ysxx = await CProManageRule.GetYSBGByid(bgid);
                return Success(ysxx);
            }
            catch (Exception ex)
            {
                return Fail("响应失败" + ex);
            }
        }

        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> Getysbgbyysid(string ysid)
        {
            try
            {
                var ysxx = await CProManageRule.GetYSBGByysid(ysid);
                return Success(ysxx);
            }
            catch (Exception ex)
            {
                return Fail("响应失败" + ex);
            }
        }

        [HttpPost]
        //[AllowAnonymous]
        public async Task<IActionResult> InsertCProYSBG([FromBody] xmysBGCRUD cplanjson)//dynamic cplanjson
        {
            string strope = "add";
            CProManCZBKbg cpro = new CProManCZBKbg();


            //添加附件上传
            FileUploadent filecodes = new FileUploadent();
            try
            {
                //JObject job = (JObject)cplanjson;

                //获取方法
                strope = cplanjson.ope ?? "";
                strope = strope.ToLower();

                cpro = cplanjson.ysbg;
                if (null != cplanjson.apiargs)
                    filecodes = cplanjson.apiargs;

                //获取实体类 这样复杂类型不用定义实体类
                //cpro = job["cplan"].ToObject<CProManCZBK>();

                ////接收附件列表
                //if (null != job["apiargs"])
                //    filecodes = job["apiargs"].ToObject<FileUploadent>();

                if (strope != "add" && strope != "edit" && strope != "submit")
                {
                    return Fail("参数ope 不合法");
                }
            }
            catch (Exception ex)
            {
                return Fail("参数转换出错：" + ex.Message);
            }

            try
            {
                //if (string.IsNullOrEmpty(cpro.xmid))
                //{
                //    return Fail("项目id不能为空");
                //}
                if (null == cpro.nd)
                {
                    return Fail("预算年度nd不能为空");
                }

                string strlsope = strope;
                if (strope == "submit")
                {
                    if (string.IsNullOrEmpty(cpro.id))//完成新增
                    {
                        strope = "add";
                    }
                    else
                    {
                        CProManCZBKbg jhe = await TranstionRule.FindEntityByKey<CProManCZBKbg>(cpro.id);
                        if (null == jhe)
                        {
                            strope = "add";
                        }
                        else
                        {
                            strope = "edit";
                        }
                    }
                    cpro.status = BaseEditPower.GetSPEnumValue("待审核");
                }

                if (strope == "add")
                {

                    string strxmid = CommonHelper.GetUniqueId().ToString();
                    cpro.id = strxmid;

                    cpro.tbr = GetUserName();
                    cpro.rq = DateTime.Now;

                    //项目编码
                    cpro.ysbgbm = await JavaApi.BusinessBMJavaAPI(BaseEditPower.listTableName[BaseEditPower.TableName.项目预算变更.ToString()], Newtonsoft.Json.JsonConvert.SerializeObject(cplanjson), await GetICApiInfo());
                    cpro.createuser = GetUserName();

                    if (strlsope != "submit")
                        cpro.status = BaseEditPower.GetSPEnumValue("待提交");

                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(cpro.id, filecodes, await GetICApiInfo());

                    bool bsp = true;
                    if (strlsope == "submit")
                    {
                        bsp = false;

                        var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.项目预算变更.ToString(), cpro.id);

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = cpro.ysbgbm, amount = cpro.je?.ToString() ?? "", content = "[项目预算]" + "", deptName = GetUserDeptName() };

                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.项目预算变更.ToString(), cpro.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(cplanjson), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);

                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());
                        cpro.spl = strInstanceId;
                        //if (!bsp)
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    await BusinessRule.CProManageRule.InsertCProYSbg(cpro, "add");

                    return Success("成功");
                }
                else if (strope == "edit")
                {
                    if (string.IsNullOrEmpty(cpro.id))
                    {
                        return Fail("id不能为空");
                    }
                    if (!await BaseEditPower.GetEditPower(cpro.id, BaseEditPower.TableName.项目预算变更))
                    {
                        return Fail("响应失败:请确认数据权限");
                    }


                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(cpro.id, filecodes, await GetICApiInfo());

                    bool bsp = true;
                    if (strlsope == "submit")
                    {
                        bsp = false;

                        var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.项目预算变更.ToString(), cpro.id);

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = cpro.ysbgbm, amount = cpro.je?.ToString() ?? "", content = "[项目预算变更]" + "", deptName = GetUserDeptName() };

                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.项目预算变更.ToString(), cpro.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(cplanjson), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);

                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());
                        cpro.spl = strInstanceId;
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    await BusinessRule.CProManageRule.InsertCProYSbg(cpro, "edit");

                    return Success("成功");
                }


                else
                {
                    return Fail("ope：" + strope + " ,无法识别");
                }
            }
            catch (Exception ex)
            {
                return Fail("响应失败:" + ex.Message);
            }
        }

        /// <summary>
        /// 根据id删除多条数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> DeleteysbgMulti(Root ids)
        {
            try
            {
                foreach (BaseDeleteMulti idm in ids.ids)
                {
                    if (!await BaseEditPower.GetEditPower(idm.id, BaseEditPower.TableName.项目预算变更))
                    {
                        throw new Exception("请确认数据权限");
                    }
                }

                await BusinessRule.CProManageRule.DeleteYSbgMulti(ids);

                return Success("成功");
            }
            catch (Exception ex)
            {
                //_logger.Error(ids.ToJson() + "||" + ex.Message);
                return Fail(ex.Message);
            }
        }



        #endregion

        #region 导入
        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> Getimportjsdw()
        {
            List<JavaApi.DataItem> list = await GetDepInfoList();
            try
            {
                await TranstionRule.GetRunActionTransAsync(async irp =>
                {

                    string strsql = "delete from BGLBMINFOS ";

                    await irp.ExecuteSql(strsql);

                    for (int i = 0; i < list.Count; i++)
                    {
                        string id = list[i].deptId;
                        string code = list[i].code;
                        string name = list[i].name;

                        strsql = "insert into BGLBMINFOS(id,code,vvv) values('" + id + "','" + code + "','" + name + "')";

                        //TranstionRule.ProcessSql(strsql);

                        await irp.ExecuteSql(strsql);

                    }

                    return true;
                });

                return Success("成功");
            }
            catch (Exception)
            {
                return Fail("失败");
            }


        }

        [HttpGet]
        //[AllowAnonymous]
        public async Task<IActionResult> Getimport(string tname)
        {
            try
            {
                if (string.IsNullOrEmpty(tname))
                {
                    tname = "项目";
                }

                DataTable table = await TranstionRule.ProcessSql("select * from " + "" + tname);
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    await TranstionRule.GetRunActionTransAsync(async irp =>
            {
                //for (int i = 0; i < table.Rows.Count; i++)
                //{
                string str4 = table.Rows[i]["项目名称"].ToString();

                //项目名称不能为空
                if (!string.IsNullOrEmpty(str4))
                {
                    //项目编码
                    string strxmbm = table.Rows[i]["项目编号"].ToString();
                    //string strxmbm = table.Rows[i]["项目编号"].ToString();

                    string strjsdw = table.Rows[i]["建设单位"].ToString();
                    //string strdtmc = table.Rows[i]["单体名称"].ToString();

                    string str3 = table.Rows[i]["合同编号"].ToString();
                    str3 = string.IsNullOrEmpty(str3) ? CommonHelper.GetUniqueId().ToString() : str3;
                    string strxmflname = table.Rows[i]["项目类型"].ToString();

                    string strhtmc = table.Rows[i]["合同名称"].ToString();
                    string str6 = table.Rows[i]["施工单位"].ToString();


                    string p = table.Rows[i]["合同金额"].ToString();

                    string strzjly = "";

                    string strlxje = table.Rows[i]["立项金额"].ToString();

                    string str10 = DateTime.Now.AddYears(-1).ToString("yyyy-MM-dd");// table.Rows[i]["合同签订日"].ToString();
                    string str11 = table.Rows[i]["计划开工日期"].ToString();
                    string str12 = table.Rows[i]["计划竣工日期"].ToString();

                    string strhtlx = table.Rows[i]["合同类型"].ToString();

                    //string str14 = table.Rows[i]["保证金"].ToString();
                    string str15 = table.Rows[i]["招标方式"].ToString();

                    string strskhm = table.Rows[i]["收款户名"].ToString();
                    string strkhyh = table.Rows[i]["开户行"].ToString();
                    string strskzh = table.Rows[i]["收款账号"].ToString();

                    //string str16 = table.Rows[i]["决算金额"].ToString();

                    string strsjdw = table.Rows[i]["审计单位"].ToString();
                    string strjgrq = table.Rows[i]["实际竣工日期"].ToString();

                    string str17 = table.Rows[i]["审定价"].ToString();
                    string strsdrq = table.Rows[i]["审定日期"].ToString();
                    string strzfje = table.Rows[i]["已付款"].ToString();
                    string strfkrq = table.Rows[i]["付款时间"].ToString().Trim();
                    string strxmid = "";

                    //str2
                    string departid = "";
                    DataTable tabledep = await TranstionRule.ProcessSql("select id from BGlbmInfos where vvv = '" + strjsdw + "'");
                    for (int j = 0; j < tabledep.Rows.Count; j++)
                    {
                        departid = tabledep.Rows[j]["id"].ToString();
                    }


                    DataTable allZD = await TranstionRule.ProcessSql("select * from  CProManage where xmmc = '" + str4 + "' and departid = '" + departid + "'");
                    for (int j = 0; j < allZD.Rows.Count; j++)
                    {
                        strxmid = allZD.Rows[j]["id"].ToString();
                    }
                    Transaction transaction = new Transaction();

                    string dtid = "";

                    if (string.IsNullOrEmpty(strxmid))//string.IsNullOrEmpty(str21)
                    {
                        strxmid = CommonHelper.GetUniqueId().ToString();
                        CProManage manage = new CProManage
                        {
                            id = strxmid,
                            remark = strxmbm,

                            TotalMoney = CommonHelper.ParseDecimal(strlxje),
                            xmbm = strxmbm,//await JavaApi.BusinessBMJavaAPI(BaseEditPower.listTableName[BaseEditPower.TableName.项目.ToString()], Newtonsoft.Json.JsonConvert.SerializeObject(table), await GetICApiInfo()),
                            xmmc = str4,
                            xmsx = "项目属性",
                            BuildUnit = strjsdw,
                            zjly = strzjly,
                            levels = "高",
                            status = "2",
                            departid = departid,
                            xmflname = strxmflname
                        };

                        //新增项目
                        await irp.Insert(manage);


                        string strdxlx = "protect_section_type";// ConfigHelper.GetAppSettings("ServerOp:dxlx");
                        string stradmin = ConfigHelper.GetAppSettings("ServerOp:SerAdmin");
                        //List<JavaApi.DataItemDIC> list = await JavaApi.GetDicJavaAPI(strdxlx, await GetICApiInfo(stradmin));

                        List<JavaApi.DataItemDIC> list = new List<JavaApi.DataItemDIC>() { new JavaApi.DataItemDIC() { value = "测试" } };
                        //protect_section_type

                        for (int lx = 0; lx < list.Count; lx++)
                        {
                            CProManageLX elx = new CProManageLX
                            {
                                id = CommonHelper.GetUniqueId().ToString(),
                                xmid = manage.id,
                                pid = "0",
                                dxmc = list[lx].value.ToString(),
                                status = "2",
                                createdate = DateTime.Now,
                                createuser = ""
                            };

                            //pid赋值
                            dtid = elx.id;

                            //新增单体
                            await irp.Insert(elx);
                        }
                    }
                    else
                    {
                        string sql = "select * from CProManageLX where xmid = '" + strxmid + "' and pid = '0' ";
                        DataTable dtsdt = await TranstionRule.ProcessSql(sql); ;
                        for (int num = 0; num < dtsdt.Rows.Count; num++)
                        {
                            dtid = dtsdt.Rows[num]["id"].ToString();
                            break;
                        }
                    }

                    string strdxid = CommonHelper.GetUniqueId().ToString();
                    CProManageLX elx2 = new CProManageLX
                    {
                        id = strdxid,
                        dxbm = await JavaApi.BusinessBMJavaAPI(BaseEditPower.listTableName[BaseEditPower.TableName.单项.ToString()], Newtonsoft.Json.JsonConvert.SerializeObject(table), await GetICApiInfo()),
                        xmid = strxmid,
                        pid = dtid,
                        dxmc = strhtmc,
                        status = "2",
                        createdate = DateTime.Now
                    };

                    //添加标段
                    await irp.Insert(elx2);

                    CProZB ozb = new CProZB
                    {
                        id = CommonHelper.GetUniqueId().ToString(),
                        dxid = strdxid,
                        dxnm = strhtmc,
                        bdje = CommonHelper.ParseDecimal(p),
                        zbfs = str15,
                        status = "2",
                        createdate = DateTime.Now
                        //,
                        //////////////这里是中标信息//////////////////////
                        //wtbm = str6,
                        //wtrq = Common.ParseDateTime(str10)//合同签订日期

                    };

                    await irp.Insert(ozb);

                    CProContract contract = new CProContract
                    {
                        id = CommonHelper.GetUniqueId().ToString(),
                        htbh = str3,
                        dxid = strdxid,
                        htmc = strhtmc,

                        jf = strjsdw,
                        yf = str6,
                        htrq = string.IsNullOrEmpty(str10) ? DateTime.Now : CommonHelper.ParseDateTime(str10),
                        kgrq = string.IsNullOrEmpty(str11) ? DateTime.Now : CommonHelper.ParseDateTime(str11),
                        jgrq = string.IsNullOrEmpty(str12) ? DateTime.Now : CommonHelper.ParseDateTime(str12),
                        htxz = "",
                        htlx = strhtlx,
                        htje = CommonHelper.ParseDecimal(p),
                        status = "2",
                        createdate = DateTime.Now
                    };
                    await irp.Insert(contract);

                    CProContractSF jf = new CProContractSF();
                    jf.id = CommonHelper.GetUniqueId().ToString();
                    jf.htid = contract.id;
                    jf.lx = "jf";
                    jf.dwmc = strjsdw;

                    await irp.Insert(jf);

                    CProContractSF yf = new CProContractSF();
                    yf.id = CommonHelper.GetUniqueId().ToString();
                    yf.htid = contract.id;
                    jf.lx = "yf";
                    yf.dwmc = str6;

                    await irp.Insert(yf);

                    BConstructionUnitsYH yhzh = new BConstructionUnitsYH();
                    yhzh.id = CommonHelper.GetUniqueId().ToString();
                    yhzh.htid = contract.id;
                    yhzh.dwmc = strskhm;
                    yhzh.yhmc = strkhyh;
                    yhzh.yhzh = strskzh;

                    await irp.Insert(yhzh);

                    if (CommonHelper.ParseDecimal(strzfje) > 0)
                    {
                        string str24 = CommonHelper.GetUniqueId().ToString();
                        CProFKSQ ofksq = new CProFKSQ
                        {
                            id = str24,
                            fkbm = await JavaApi.BusinessBMJavaAPI(BaseEditPower.listTableName[BaseEditPower.TableName.付款申请.ToString()], Newtonsoft.Json.JsonConvert.SerializeObject(table), await GetICApiInfo()),
                            htid = contract.id,
                            sqje = CommonHelper.ParseDecimal(strzfje),

                            kpje = 0,
                            yfk = "0",
                            status = "2",
                            skzhmc = strskhm,
                            skyh = strkhyh,
                            skzh = strskzh,

                            createdate = DateTime.Now
                        };

                        if (!string.IsNullOrEmpty(strfkrq))
                            ofksq.sqrq = CommonHelper.ParseDateTime(strfkrq);
                        else
                            ofksq.sqrq = DateTime.Now;

                        await irp.Insert(ofksq);

                        CProFKMX ofkmx = new CProFKMX
                        {
                            id = CommonHelper.GetUniqueId().ToString(),
                            sqid = str24,
                            zfje = CommonHelper.ParseDecimal(strzfje),
                            zffs = "转账",
                            zfrq = CommonHelper.ParseDateTime(strfkrq),
                            fkdw = strjsdw,
                            skdw = str6,
                            zjxz = "",
                            createdate = DateTime.Now,
                            bz = ""
                        };
                        if (!string.IsNullOrEmpty(strfkrq))
                            ofkmx.zfrq = CommonHelper.ParseDateTime(strfkrq);
                        else
                            ofkmx.zfrq = DateTime.Now;

                        await irp.Insert(ofkmx);
                    }

                    if (!string.IsNullOrEmpty(strjgrq))
                    {
                        CProJG ojg = new CProJG
                        {
                            id = CommonHelper.GetUniqueId().ToString(),
                            htid = contract.id,
                            jgrq = CommonHelper.ParseDateTime(strjgrq),
                            status = "2",
                            createdate = DateTime.Now
                        };

                        await irp.Insert(ojg);
                    }

                    if (CommonHelper.ParseDecimal(str17) > 0)
                    {
                        CProJS ojs = new CProJS
                        {
                            id = CommonHelper.GetUniqueId().ToString(),
                            htid = contract.id,
                            sjdw = strsjdw,
                            ssje = CommonHelper.ParseDecimal(str17),
                            sjje = 0M,
                            sjf = 0M,
                            sdje = CommonHelper.ParseDecimal(str17),
                            jszt = "2",
                            status = "2",
                            createdate = DateTime.Now
                        };
                        await irp.Insert(ojs);
                    }
                }



                //}
                return true;
            });

                }

                //return true;
                return Success("成功");
            }

            catch (Exception ex)
            {
                return Fail("响应失败:" + ex.Message);
            }
        }

        #endregion


    }
}